 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('采购申请头文件')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
	<!-- <style type="text/css">.table {table-layout:fixed;}</style> -->
</head>

<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="proPurchRequestItemForm" th:action="@{/proPurchRequestItem/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3" style="width: 33%">
                               <label>部门：</label>
                               <select id="purchRequestDept" name="purchRequestDept" sysDict="BM" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                              <label>成本中心：</label>
                              <select id="costCenter" name="costCenter" sysDict="CBZX" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
	                            <label>采购申请单号：</label>
	                            <input type="text" name="purchRequestCode"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <div>
	                            <label>申请者：</label>
	                            <input type="text" name="purchRequestStaff"/>
                            </div>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>采购组：</label>
                            <select id="costCenter" name="purchGroup" sysDict="CGZ" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
	                        <label>工厂：</label>
	                        <select id="costCenter" name="factory" sysDict="GC" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
	                        <label>货源状态：</label>
	                        <select id="costCenter" name="goodsStatus" sysDict="HYZT" emptyFirst="true"></select>
                        </li>
                        
                        <li class="col-sm-3" style="width: 33%">
                            <button id="proPurchRequestItemSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索</button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置</button>
                            <button id="proPurchRequestItemAdvancedSearch" type="button" class="btn btn-primary btn-rounded btn-sm" style="display: none"><i class="fa fa-caret-down"></i>&nbsp;高级搜索</button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="proPurchRequestItemToolBar" role="group">
                <!-- <a class="btn btn-outline btn-success btn-rounded" onclick="toEditPurchRequestItem()">
                    <i class="fa fa-edit"></i> 货源分配
                </a> -->
                <a class="btn btn-outline btn-success btn-rounded" onclick="toinquiryBatchsubmission()">
                    <i class="fa fa-save"></i> 批量提交
                </a>
                <!-- <a class="btn btn-outline btn-success btn-rounded" data-toggle='modal' data-target='#myModalVendor' onclick="show()">
                    <i class="fa fa-save"></i> 分配货源
                </a> -->
                <a class="btn btn-outline btn-success btn-rounded" onclick="show()">
                    <i class="fa fa-save"></i> 分配货源
                </a>
            </div>
            <table id="proPurchRequestItem" data-mobile-responsive="true"></table>
        </div>
    </div>

</div>

<!-- 指定报价的供应商 -->
<div class="modal fade" id="myModalVendor" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document" style="width:70%">
        <div class="modal-content" style="height:700px">
            <form id="essentialInformationForm" th:action="@{/essentialInformation/list}" class="form-inline">
                <input type="hidden" name="materialCode" id="materialCode" />
                <input type="hidden" name="factory" id="factory" />
                <div class="modal-header">
		                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		                <h4 class="modal-title" id="myModalLabel">供应商报价信息</h4>
		         </div>
                <div class="modal-body" style="height:90%;width:85%;overflow:auto">
                    
		            <div style="height: 30px;width:100%">
					    <ul>
							<li class="col-sm-3" style="width: 40%">
								<label  style="width: 100px">供应商编码：</label>
								<input name="vendorCode" type="text" id="vendorCode" placeholder="供应商编码" class="form-control">
							</li>
							<li class="col-sm-3" style="width: 40%">
								<label  style="width: 120px">供应商名称：</label>
								<input name="vendorName" type="text" id="vendorName" class="form-control" placeholder="供应商名称">
							</li>
							<li class="col-sm-3" style="width: 20%">
								<button id="essentialInformationSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索</button>
								<button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置</button>
							</li>
						</ul>
					</div>
			       	<table class="table table-bordered text-nowrap" id="essentialInformation">
						  
					</table>
                </div>
            </form>
	            <div class="modal-footer">
	               <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
	                <button type="button" class="btn btn-primary" onclick='check_form("essentialInformation")'>确定</button>
	               <!--<button type="button" class="btn btn-primary" onclick='toEditPurchRequestItem()'>确定</button>-->
	            </div>
            </div>
        </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->

<!--  -->
<script type="text/javascript">
    //面向对象
    var searcherBS= Searcher.createBS('#proPurchRequestItem',
        [
            {radio  : true},
            {title: "货源状态", field:"goodsStatus"},
            {title: "采购申请编码", field:"purchRequestCode"},
            {title: "采购申请行项目编码",field:"purchRequestItemCode"},
            {title: "采购申请类型",field:"purchRequestType"},
            {title: "创建人",field:"createdBy"},
            {title: "采购申请行项目编码",field:"purchRequestItemCode"},
            {title: "申请部门",field:"purchRequestDept"},
            {title: "科目类别",field:"subjectCategory"},
            {title: "采购组",field:"purchGroup"},
            {title: "采购组织",field:"purchOrg"},
            {title: "使用人",field:"usePeople"},
            {title:"物料号",field:"materialCode"},
            {title: "物料描述",field:"materialDesc"},
            {title: "工厂",field:"factory"},
            {title: "库存地点",field:"storageLocation"},
            {title: "物料组",field:"materialGroup"},
            {title: "申请数量",field:"applyQty"},
            {title: "计量单位",field:"measureUnit"},
            {title: "成本中心",field:"costCenter"},
            {title: "内部订单号",field:"internalOrderCode"},
            {title: "主资产号",field:"mainAssetsCode"},
            {title: "资产次级编号",field:"assetsSencondCode"},
            {title: "需求日期",field:"requestDate"},
            {title: "交货日期",field:"deliveryDate"},
            {title: "交货地址",field:"deliveryAddress"},
            {title: "供应商编码",field:"vendorTmpCode"},
            {title: "供应商名称",field:"vendorName"},
            {title: "含税单价",field:"taxPrice"},
            {title: "价格单位",field:"priceUnit"},
            {title: "货币",field:"currency"},
            {title: "税码",field:"taxCode"}
        ]
    	
    );
    //刷新数据
    function refresh() {
    	searcherBS.refresh();
    }
    
    // 初始化时间插件
    laydate.render({elem: '#requestDate'});
    laydate.render({elem: '#deliveryDate'});
    
    initDictDataSel();//初始化所有字典数据
    /**
     * 跳转至新增页
     */
    function toAddnproPurchRequestHeader() {
        var index=Dialog.openUrl("新增采购订单", ctxPath + '/modules/requisition/requisition_add.html',{area : ['900px','700px'],
            maxmin: true,
            end: function() {}
        });
        layer.full(index);
    }
    function show(){
    	//塞条件
    	var rows = $("#proPurchRequestItem").bootstrapTable('getSelections');
    	for (var i=0;i<rows.length;i++){
    		var  factory = rows[i].factory;
    		var materialCode = rows[i].materialCode;
    		$("#factory").val(factory);
    		$("#materialCode").val(materialCode);
		}
    	//check_form("essentialInformation");
    	check_form("proPurchRequestItem");
   }
    
  function check_form(obj){
		//使用getSelections即可获得，row是json格式的数据
	    var rows = $("#"+obj).bootstrapTable('getSelections');
	    var rowDatas=new Array();
		//判断是否选中行
		if (!rows || rows.length == 0) {
		   Dialog.alert("请先选中一行！")
		   return;
		}
		//遍历选中的值
		for (var i=0;i<rows.length;i++){
			if(rows[i].goodsStatus==-1){
				
				rowDatas.push(rows[i].materialCode+";"+rows[i].werks+";"+rows[i].srmCode);
				toEditPurchRequestItem(rowDatas);
			}else{
				if(rows[i].goodsStatus==-2){
					return Dialog.alert("请先批量提交");
				}
				if(rows[i].goodsStatus==0){
					return Dialog.alert("货源已经指定");
				}
			}
		}
		
	}
    /**
     * 编辑多条信息
     */
    function toEditPurchRequestItem(rowDatas1) {
    	var rowDatas = searcherBS.getSelectedRows("pkId");
        if (rowDatas.length < 1) {
            Dialog.errorMsg("请至少选择一行进行编辑");
            return;
        }
       
       // itemBS.getData({'url': ctxPath + '/nproPurchRequestItem/list?purchRequestCode='+row.purchRequestCode});
        var index = Dialog.openUrl("货源分配信息", ctxPath + '/proPurchRequestItem/toAllot/?ids='+rowDatas+'&inqids='+rowDatas1,
    		    {area : ['900px','700px'],
    			  maxmin: true,
    		      end: function () {}
    		    });
    		layer.full(index)

    }
    
    
    function deletePurchRequestHeader() {
        //var rowDatas = searcherBS.getSelectedRows("pkId");
        var rowDatas = searcherBS.getSelectedRows1("pkId");
        if (rowDatas.length < 1) {
            Dialog.errorMsg("请至少选择一行");
            return;
        }
        Dialog.confirm('确定删除？', function(index){
            Page.ajaxPOST(ctxPath + "/proPurchRequestHeader/delete", {userIds: rowDatas}, function () {
                Dialog.msg("操作成功", function () {
                    searcherBS.refresh();
                });
            });
        });
     }
    //批量提交
    function toinquiryBatchsubmission(){
    	 var rowData = searcherBS.getSelectedRows("pkId");
    	 var rowDatas = $("#proPurchRequestItem").bootstrapTable('getSelections');
    	 var goodsStatus;
    	 for(var i=0;i<rowDatas.length;i++){
    		 goodsStatus = rowDatas[i].goodsStatus;
    	 }
    	if(goodsStatus==-2){
    		 var data = {"pkid":rowData};
    		 alert(data);
        	 $.ajax({
    				type: "POST",
    				url: ctxPath + "/proPurchRequestItem/insertBatch",
    				data: JSON.stringify(data),//抬头id
    				contentType : "application/json;charsetset=UTF-8",//必须
    				dataType:"json",//必须
    				cache: true,
    				async: false,
    				error: function (request) {
    				      showTipAlert("Connection error");
    				},
    				success: function (data) {
    					if(data.result="ok"){
    						Dialog.msg("批量提交成功!",function(){
    			                   // Dialog.closeFrame(window.name)
    			                    searcherBS.refresh();
    			            });
    					}
    					if(data.result="err"){
    						Dialog.msg("批量提交失败!",function(){
    			                   // Dialog.closeFrame(window.name)
    			                    searcherBS.refresh();
    			            });
    					}
    				}
    		 });
    	}else{
    		return;
    	}
    }
    
</script>
</body>
</html>